<?php

namespace app\admin\command;

use app\common\library\job\TeamCountExpertLevelJob;
use app\common\library\job\TeamCountJob;
use app\common\library\TeamCount;
use app\common\library\token\driver\Redis as DriverRedis;
use app\common\library\User;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\Log;
use think\cache\driver\Redis;
use think\Queue;


class MonthWeightingScore extends Command
{
    // 休眠间隔
    protected $_sleep = 30;
    // 是否有数据正在处理
    protected $_doing = false;
    //执行任务数组
    protected $_tasks = [];
    //缓存key
    protected $cache_key = "";

    protected function configure()
    {
        $this->setName('MonthWeightingScore')->setDescription('每月加权分红');
    }

    /**
     * 休眠
     *
     * @param string $second            
     */
    public function sleep($second = "")
    {
        $second = $second ? $second : $this->_sleep;
        sleep(sleep($second)); // TP5的命令行 sleep($second) 不生效
        echo "sleep {$second} seconds ok!time:" . date('Y-m-d H:i:s') . PHP_EOL;
    }


    protected function execute(Input $input, Output $output)
    {
        $this->autoRun();
    }

    /**
     * 自动执行
     *
     * @return bool
     */
    protected function autoRun()
    {
        $this->RunType(1, 1);
        $this->RunType(2, 1);
        $this->RunType(3, 1);
        $this->RunType(4, 1);

        //区经理商类型：按区经理商个数平均处理
        $this->RunType2(5, 1);

        echo "finished all" . PHP_EOL;
    }


    /**
     * 按类型执行
     *
     * @return bool
     */
    protected function RunType($level, $type)
    {
        $month = date("Ym");
        $result = $this->getWorks($level);
        // exit(print_r($result));
        $list = $result["list"];
        $total_money = $result["total_money"];
        if ($list) {
            foreach ($list as $val) {
                $percent = intval($val["total_money"] * 100 / $total_money);
                $item = db("user_weighting_score_detail")->where("user_id", $val["user_id"])->where("level", $level)->where("type", $type)->where("month", $month)->find();
                if ($item) {
                    $data = [
                        "percent" => $percent,
                    ];
                    db("user_weighting_score_detail")->where("id", $item["id"])->update($data);
                } else {
                    $data = [
                        "user_id" => $val["user_id"],
                        "level" => $level,
                        "type" => $type,
                        "percent" => $percent,
                        "month" =>  $month,
                        "add_time" => time(),
                    ];
                    db("user_weighting_score_detail")->insert($data);
                }
            }
        }
    }

    /**
     * 按类型执行
     *
     * @return bool
     */
    protected function RunType2($level, $type)
    {
        $month = date("Ym");
        $result = $this->getWorks($level);
        $list = $result["list"];
        $total_money = $result["total_money"];
        if ($list) {
            foreach ($list as $val) {
                $percent = 1 * 100 / count($list);
                $item = db("user_weighting_score_detail")->where("user_id", $val["user_id"])->where("level", $level)->where("type", $type)->where("month",  $month)->find();
                if ($item) {
                    $data = [
                        "percent" => $percent,
                    ];
                    db("user_weighting_score_detail")->where("id", $item["id"])->update($data);
                } else {
                    $data = [
                        "user_id" => $val["user_id"],
                        "level" =>  $level,
                        "type" => $type,
                        "percent" => $percent,
                        "month" =>  $month,
                        "add_time" => time(),
                    ];
                    db("user_weighting_score_detail")->insert($data);
                }
            }
        }
    }


    /**
     * 获取任务
     */
    protected function getWorks($level)
    {
        try {

            //三星达人
            if ($level == 1) {
                $list = db("user_team_count")->where("myself_expert_level", 3)->select();
                $total_money = db("user_team_count")->where("myself_expert_level", 3)->sum("total_money");
            }
            //市场总监
            if ($level == 2) {
                $list = db("user_team_count")->where("myself_marketing_level", 1)->select();
                $total_money = db("user_team_count")->where("myself_marketing_level", 1)->sum("total_money");
            }
            //市场经理
            if ($level == 3) {
                $list = db("user_team_count")->where("myself_marketing_level", 2)->select();
                $total_money = db("user_team_count")->where("myself_marketing_level", 2)->sum("total_money");
            }
            //合伙人
            if ($level == 4) {
                $list = db("user_team_count")->where("myself_marketing_level", 3)->select();
                $total_money = db("user_team_count")->where("myself_marketing_level", 3)->sum("total_money");
            }
            //区经理商
            if ($level == 5) {
                $list = db("user_team_count")->where("myself_area_manager", 1)->select();
                $total_money = db("user_team_count")->where("myself_area_manager", 1)->sum("total_money");
            }

            return ["list" => $list, "total_money" => $total_money];
        } catch (Exception $e) {
            Log::write($e->getMessage());
        }
    }
}
